import { _decorator, Animation, Component, EventTouch, misc, Node, tween, v3, Vec3 } from 'cc';
import { BulletType, gameConfig } from './Configs';
import { PlayerLayers } from './PlayerLayers';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {

    private _playerLayers: Node = null;
    index: number = 0;
    state: string = "none"
    isGunMove: Boolean = false;
    gunRotationSpeed: number = 50;
    private _playerLayersTs: PlayerLayers = null;

    private _modelAni: Animation = null;
    get modelAni() {
        if (!this._modelAni) {
            let model: Node = this.node.getChildByName("Model");
            this._modelAni = model.getComponent(Animation);
        }
        return this._modelAni;
    }

    private _gun: Node = null;

    get gun() {
        if (!this._gun) {
            this._gun = this.node.getChildByName("Gun");
        }
        return this._gun;
    }

    //枪是否开始旋转
    private _isTouchStartGun: boolean = false;

    private _isClick: boolean = false;

    protected onLoad(): void {
        this._playerLayers = this.node.parent;
    }

    protected start(): void {
        this.changeState("RoleRun");
        tween(this.node)
            .by(2, { position: v3(500, 0, 0) })
            .call(() => {
                this.changeState("RoleWait");
                gameConfig.isScroll = true;
                this.scheduleOnce(() => {
                    gameConfig.isScroll = false;
                    let playerLayersTs = this._playerLayers.getComponent(PlayerLayers);
                    playerLayersTs.sendMsg("createEnemy", "EnemyLayers");
                }, 2)
            })
            .start()
    }
    protected update(dt: number): void {
        if (this._isTouchStartGun) {
            this.gunRotation(dt);
        }
    }
    init(index: number) {
        this.index = index;
        this._playerLayersTs = this._playerLayers.getComponent(PlayerLayers);
    }
    //状态机，实现玩家的状态切换
    changeState(state: string) {
        //如果状态和上一个状态一致就不切换
        if (this.state == state) {
            return;
        }
        this.state = state;

        if (this.state == "RoleStop") {
            this.modelAni.stop();
            return;
        }
        this.modelAni.stop();
        this.modelAni.play(this.state + this.index);
    }
    isStartRotation(isClick) {
        this._isClick = isClick;
    }
    //点击开始
    onTouchStart(event: EventTouch) {
        if (!this._isClick) {
            return;
        }
        this._isTouchStartGun = true;

    }

    //点击结束
    onTouchEnd(event: EventTouch) {
        if (this._isClick) {
            this._playerLayersTs.sendMsg("clearPath", "DrawLayer");
            this._isClick = false;
            this._isTouchStartGun = false;
            let fireNode = this.gun.getChildByName("FireNode");
            let fireNodeWorldPos = fireNode.worldPosition;
            //枪旋转的角度设置为子弹的初始角度，并将角度转成弧度制
            let radians = misc.degreesToRadians(this.gun.angle);
            //初始化方向
            let dir = v3();
            //将弧度转换成方向
            //通过枪的角度设置方向   结果存入dir  起始位置(即x轴) 旋转中心(是绕哪个轴旋转的)  旋转弧度（不是角度）
            Vec3.rotateZ(dir, v3(1, 0, 0), v3(0, 0, 1), radians);
            this._playerLayersTs.sendMsg("addBullet", "BulletLayers", fireNodeWorldPos, dir, BulletType.Player, "Role" + this.index);
            tween(this.gun)
                .to(0.5, { angle: 0 })
                .start()
        }

    }

    //枪开始旋转
    gunRotation(dt: number, ...rest: any[]) {
        this.gun.angle += this.gunRotationSpeed * dt;
        //绘制路径
        let fireNode = this.gun.getChildByName("FireNode");
        let fireNodeWorldPos = fireNode.worldPosition;
        //枪旋转的角度设置为子弹的初始角度，并将角度转成弧度制
        let radians = misc.degreesToRadians(this.gun.angle);
        //初始化方向
        let dir = v3();
        //将弧度转换成方向
        //通过枪的角度设置方向   结果存入dir  起始位置(即x轴) 旋转中心(是绕哪个轴旋转的)  旋转弧度（不是角度）
        Vec3.rotateZ(dir, v3(1, 0, 0), v3(0, 0, 1), radians);
        this._playerLayersTs.sendMsg("drawPath", "DrawLayer", fireNodeWorldPos, dir, dt);
        if (this.gun.angle >= 90 || this.gun.angle <= 0) {
            this.gun.angle = (this.gun.angle >= 90 ? 90 : 0);
            this.gunRotationSpeed = -this.gunRotationSpeed;
        }
    }

    run() {
        this.changeState("RoleRun");
        gameConfig.isScroll = true;
        this.scheduleOnce(() => {
            this.changeState("RoleWait");
            gameConfig.isScroll = false;
            this._playerLayersTs.sendMsg("createEnemy", "EnemyLayers");
        }, 2)
    }
}


